<?php

/**
 * category actions.
 *
 * @package    jobeet
 * @subpackage category
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class categoryActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->bachkim_categorys = Doctrine_Core::getTable('BachkimCategory')
      ->createQuery('a')
      ->execute();
  }  
  
   // san pham
    public function executeProduct(sfWebRequest $request)
    {
         $this->cate = Doctrine_core::getTable('BachkimCategory')->getCategory1('sanpham',0);
         $this->forward404Unless($this->cate);        
    }
    
    
     // dich vu   
    public function executeService(sfWebRequest $request)
    {
         $this->cate = Doctrine_core::getTable('BachkimCategory')->getCategory1('dichvu',0);
         $this->forward404Unless($this->cate);  
    } 
    
     // Tai nguyen  
    public function executeResource(sfWebRequest $request)
    {
         $this->cate = Doctrine_core::getTable('BachkimCategory')->getCategory1('tainguyen',0);
         $this->forward404Unless($this->cate);  
    } 
    
    
    
         // Gioi thieu 
    public function executeIntroduce(sfWebRequest $request)
    {
         $this->cate = Doctrine_core::getTable('BachkimCategory')->getCategory2('gioithieuchung');
         $this->forward404Unless($this->cate);      
    }  
    
     // Thong bao 
    public function executeNotice(sfWebRequest $request)
    {
         $this->cate = Doctrine_core::getTable('BachkimCategory')->getCategory2('thongbao');
          
         foreach($this->cate as $category):;     
             
                // luu vao session phuc vu viec hien thi 1 tin moi nhat
                    $parent_id = $this->getUser()->setAttribute('parent_id', $category->getId());
                    
                // phan trang
                    $this->pager = new sfDoctrinePager(
                        'BachkimArtice',
                        sfConfig::get('app_max_artices_on_category')
                      );
                    $this->pager->setQuery($category->getWithArticeQuery());
                    $this->pager->setPage($request->getParameter('page', 1));
                    $this->pager->init();
         endforeach;   
    }       
      
     // Tin tuc 
    public function executeNewartice(sfWebRequest $request)
    {
         // da ngon ngu
         if (!$request->getParameter('sf_culture'))
          {
            if ($this->getUser()->isFirstRequest())
            {
              $culture = $request->getPreferredCulture(array('vi', 'en'));
              $this->getUser()->setCulture($culture);
              $this->getUser()->isFirstRequest(false);
            }
            else
            {
              $culture = $this->getUser()->getCulture();
            }
         
            $this->redirect('@localized_homepage');
          }
         // ---------------
         
         
         $this->cate = Doctrine_core::getTable('BachkimCategory')->getCategory2('tintuc');
         
         foreach($this->cate as $category):;
         
                    // luu vao session phuc vu viec hien thi 1 tin moi nhat
                    $parent_id = $this->getUser()->setAttribute('parent_id', $category->getId());
                    
                  // phan trang  
                    $this->pager = new sfDoctrinePager(
                        'BachkimArtice',
                        sfConfig::get('app_max_artices_on_category')
                      );
                    $this->pager->setQuery($category->getWithArticeQuery());
                    $this->pager->setPage($request->getParameter('page', 1));
                    $this->pager->init();
         endforeach;  
    }
      
      
      
                         
  public function executeShow(sfWebRequest $request)
  {
    $this->category = Doctrine_Core::getTable('BachkimCategory')->find(array($request->getParameter('id')));
    //$this->forward404Unless($this->category);
    
    foreach($this->category as $category):;
        // luu vao session phuc vu viec hien thi 1 tin moi nhat
                $parent_id = $this->getUser()->setAttribute('parent_id', $this->category->getId());
        
        // phan trang
        $this->pager = new sfDoctrinePager(
            'BachkimArtice',
            sfConfig::get('app_max_artices_on_category')
          );
        $this->pager->setQuery($this->category->getWithArticeQuery());
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();  
     endforeach;   
   
  }

  public function executeListartice(sfWebRequest $request)
  {
    $this->category = Doctrine_Core::getTable('BachkimCategory')->find(array($request->getParameter('id')));
    //$this->forward404Unless($this->category);
    
    foreach($this->category as $category):;
        // luu vao session phuc vu viec hien thi 1 tin moi nhat
                $parent_id = $this->getUser()->setAttribute('parent_id', $this->category->getId());
        
        // phan trang
        $this->pager = new sfDoctrinePager(
            'BachkimArtice',
            sfConfig::get('app_max_artices_on_category')
          );
        $this->pager->setQuery($this->category->getWithArticeQuery());
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->init();  
     endforeach;           
  } 
  
  

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new BachkimCategoryForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BachkimCategoryForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($bachkim_category = Doctrine_Core::getTable('BachkimCategory')->find(array($request->getParameter('id'))), sprintf('Object bachkim_category does not exist (%s).', $request->getParameter('id')));
    $this->form = new BachkimCategoryForm($bachkim_category);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($bachkim_category = Doctrine_Core::getTable('BachkimCategory')->find(array($request->getParameter('id'))), sprintf('Object bachkim_category does not exist (%s).', $request->getParameter('id')));
    $this->form = new BachkimCategoryForm($bachkim_category);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($bachkim_category = Doctrine_Core::getTable('BachkimCategory')->find(array($request->getParameter('id'))), sprintf('Object bachkim_category does not exist (%s).', $request->getParameter('id')));
    $bachkim_category->delete();

    $this->redirect('category/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $bachkim_category = $form->save();

      $this->redirect('category/edit?id='.$bachkim_category->getId());
    }
  }
}
